unit uMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Objects, FMX.StdCtrls,
  FMX.Effects, System.Actions, FMX.ActnList, FMX.TabControl, FMX.ListBox,
  FMX.Layouts, FMX.Media, FMX.Graphics, FMX.Edit, FMX.ExtCtrls;

type
  TSoundType = (stMusic, stFX);

type
  TMyPopUpBoxOne = Class(TPopupBox)
  public
    procedure DoPopup;
  End;

type
  TMyPopUpBoxTwo = Class(TPopupBox)
  public
    procedure DoPopup;
  End;

type
  TfmMain = class(TForm)
    StyleBook1: TStyleBook;
    TabControl1: TTabControl;
    TabItem1: TTabItem;
    TabItem2: TTabItem;
    TabItem3: TTabItem;
    Panel1: TPanel;
    ImgLogo: TImage;
    ActionList1: TActionList;
    ChangeTabAction1: TChangeTabAction;
    Panel2: TPanel;
    ImgPlayerOne: TImage;
    ImgPlayerTwo: TImage;
    ImgFight: TImage;
    ImgPressStart: TImage;
    TmrPressStart: TTimer;
    SpeedButton1: TSpeedButton;
    ChangeTabAction2: TChangeTabAction;
    MediaPlayerMusic: TMediaPlayer;
    ImgSplash: TImage;
    Panel3: TPanel;
    TmrDelphiLogo: TTimer;
    ChangeTabAction3: TChangeTabAction;
    TabItem4: TTabItem;
    Image1: TImage;
    btnPointOne: TSpeedButton;
    btnPointTwo: TSpeedButton;
    imgPointsOneUnits: TImage;
    imgPointsTwoUnits: TImage;
    edScoreOne: TEdit;
    edScoreTwo: TEdit;
    Panel4: TPanel;
    imgBackGround: TImage;
    imgPlayerOnePicture: TImage;
    imgPlayerTwoPicture: TImage;
    imgPunchOne: TImage;
    imgPunchTwo: TImage;
    imgPointsOneDecimals: TImage;
    imgPointsTwoDecimals: TImage;
    MediaPlayerHit: TMediaPlayer;
    MediaPlayerYouWin: TMediaPlayer;
    TabItem5: TTabItem;
    imgYouWin: TImage;
    ChangeTabAction4: TChangeTabAction;
    ChangeTabAction5: TChangeTabAction;
    Label1: TLabel;
    GlowEffect1: TGlowEffect;
    GlowEffect2: TGlowEffect;
    SpeedButton2: TSpeedButton;
    TabItem6: TTabItem;
    ChangeTabAction6: TChangeTabAction;
    Image2: TImage;
    Label3: TLabel;
    Label4: TLabel;
    swMusic: TSwitch;
    swFx: TSwitch;
    btnSaveChanges: TSpeedButton;
    Label2: TLabel;
    pbServiceTwo: TProgressBar;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    pbTechniqueTwo: TProgressBar;
    pbPowerTwo: TProgressBar;
    pbCurveTwo: TProgressBar;
    LblService: TLabel;
    pbServiceOne: TProgressBar;
    LblTechnique: TLabel;
    pbTechniqueOne: TProgressBar;
    LblPower: TLabel;
    pbPowerOne: TProgressBar;
    Label11: TLabel;
    pbCurveOne: TProgressBar;
    ActionList2: TActionList;
    Image3: TImage;
    Image4: TImage;
    Label12: TLabel;
    LblTotalVictoriesOne: TLabel;
    LblTotalDefeatsTwo: TLabel;
    LblTotalDefeatsOne: TLabel;
    LblTotalVictoriesTwo: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    TotalVictoriesOne: TLabel;
    TotalDefeatsOne: TLabel;
    TotalVictoriesTwo: TLabel;
    TotalDefeatsTwo: TLabel;
    TmrStatsOne: TTimer;
    pbAggressivenessOne: TProgressBar;
    Label8: TLabel;
    Label9: TLabel;
    pbAggressivenessTwo: TProgressBar;
    TmrStatsTwo: TTimer;
    procedure TmrPressStartTimer(Sender: TObject);
    procedure ImgFightClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TmrDelphiLogoTimer(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure ImgPressStartClick(Sender: TObject);
    procedure btnPointOneClick(Sender: TObject);
    procedure btnPointTwoClick(Sender: TObject);
    procedure imgYouWinClick(Sender: TObject);
    procedure PlayMedia(Media: TMediaPlayer; FileName: String;
      SoundType: TSoundType);
    procedure ImgPlayerOneClick(Sender: TObject);
    procedure ImgPlayerTwoClick(Sender: TObject);
    procedure MyPopUpOneChange(Sender: TObject);
    procedure MyPopUpTwoChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure TmrStatsOneTimer(Sender: TObject);
    procedure TmrStatsTwoTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmMain: TfmMain;
  IntroMaxSecs, Points: Integer;
  SelectedPlayerOne, SelectedPlayerTwo: String;
  Hitted: Integer;
  MyPopUpOne: TMyPopUpBoxOne;
  MyPopUpTwo: TMyPopUpBoxTwo;
  StatsPlayerOne, StatsPlayerTwo: Array [0 .. 4] of Integer;
  cServiceOne, cTechniqueOne, cPowerOne, cCurveOne, cAggressivenessOne: Boolean;
  cServiceTwo, cTechniqueTwo, cPowerTwo, cCurveTwo, cAggressivenessTwo: Boolean;

implementation

{$R *.fmx}

uses uDataModule;
{ TfmMain }

procedure TfmMain.PlayMedia(Media: TMediaPlayer; FileName: String;
  SoundType: TSoundType);
Begin
  Media.Stop;
  Media.FileName := GetHomePath + PathDelim + 'Documents' + PathDelim +
    FileName;
  if SoundType = stMusic then
  Begin
    if swMusic.IsChecked then
      Media.Play;
  End
  Else if SoundType = stFX then
  Begin
    if swFx.IsChecked then
      Media.Play;
  End;
End;

procedure TfmMain.btnPointOneClick(Sender: TObject);
var
  YouWin: Boolean;
begin
  YouWin := False;

  // Calc score
  if (StrToInt(edScoreOne.Text) < 10) Then
  Begin
    edScoreOne.Text := IntToStr(StrToInt(edScoreOne.Text) + 1);
  end
  Else
  Begin
    if (StrToInt(edScoreOne.Text)) - (StrToInt(edScoreTwo.Text)) >= 1 then
    Begin
      imgYouWin.Visible := True;

      YouWin := True;

      if StrToInt(edScoreTwo.Text) = 0 then
        PlayMedia(MediaPlayerYouWin, 'YouWinPerfect.mp3', stFX)
      Else
        PlayMedia(MediaPlayerYouWin, 'YouWin.mp3', stFX);

      ChangeTabAction4.ExecuteTarget(Sender);
    End
    Else
    Begin
      edScoreOne.Text := IntToStr(StrToInt(edScoreOne.Text) + 1);
      // TIE BREAK
    End;
  End;

  // Change image numbers
  if StrToInt(edScoreOne.Text) >= 10 then
  Begin
    imgPointsOneDecimals.Visible := True;
    imgPointsOneUnits.MultiResBitmap.Bitmaps[1].LoadFromFile
      (GetHomePath + PathDelim + 'Documents' + PathDelim +
      IntToStr(StrToInt(edScoreOne.Text) - 10) + '.jpg');
    imgPointsOneDecimals.MultiResBitmap.Bitmaps[1].LoadFromFile
      (GetHomePath + PathDelim + 'Documents' + PathDelim + '1.jpg');
  End
  Else
  Begin
    imgPointsOneDecimals.Visible := False;
    imgPointsOneUnits.MultiResBitmap.Bitmaps[1].LoadFromFile
      (GetHomePath + PathDelim + 'Documents' + PathDelim + edScoreOne.Text
      + '.jpg');
  End;

  // Play hit sound
  if YouWin = False then
  Begin
    PlayMedia(MediaPlayerHit, 'Hit' + IntToStr(Random(6) + 1) + '.mp3', stFX);
  End;

  // Check who's serving
  Inc(Points);
  if Points < 20 then
  Begin
    if (Points mod 2) = 0 then
    Begin
      if GlowEffect1.Enabled = False then
      Begin
        GlowEffect1.Enabled := True;
        GlowEffect2.Enabled := False;
      End
      Else
      Begin
        GlowEffect1.Enabled := False;
        GlowEffect2.Enabled := True;
      End;
    End;
  End
  Else
  Begin
    if Points >= 20 then
    Begin
      if GlowEffect1.Enabled = False then
      Begin
        GlowEffect1.Enabled := True;
        GlowEffect2.Enabled := False;
      End
      Else
      Begin
        GlowEffect1.Enabled := False;
        GlowEffect2.Enabled := True;
      End;
    End;
  end;
end;

procedure TfmMain.btnPointTwoClick(Sender: TObject);
var
  YouWin: Boolean;
begin
  YouWin := False;

  // Calc score
  if (StrToInt(edScoreTwo.Text) < 10) Then
  Begin
    edScoreTwo.Text := IntToStr(StrToInt(edScoreTwo.Text) + 1);
  end
  Else
  Begin

    if (StrToInt(edScoreTwo.Text)) - (StrToInt(edScoreOne.Text)) >= 1 then
    Begin
      imgYouWin.Visible := True;

      YouWin := True;

      if StrToInt(edScoreOne.Text) = 0 then
        PlayMedia(MediaPlayerYouWin, 'YouWinPerfect.mp3', stFX)
      Else
        PlayMedia(MediaPlayerYouWin, 'YouWin.mp3', stFX);

      ChangeTabAction4.ExecuteTarget(Sender);
    End
    Else
    Begin
      edScoreTwo.Text := IntToStr(StrToInt(edScoreTwo.Text) + 1);
    End;
  End;

  // Change image numbers
  if StrToInt(edScoreTwo.Text) >= 10 then
  Begin
    imgPointsTwoDecimals.Visible := True;
    imgPointsTwoUnits.MultiResBitmap.Bitmaps[1].LoadFromFile
      (GetHomePath + PathDelim + 'Documents' + PathDelim +
      IntToStr(StrToInt(edScoreTwo.Text) - 10) + '.jpg');
    imgPointsTwoDecimals.MultiResBitmap.Bitmaps[1].LoadFromFile
      (GetHomePath + PathDelim + 'Documents' + PathDelim + '1.jpg');
  End
  Else
  Begin
    imgPointsTwoDecimals.Visible := False;
    imgPointsTwoUnits.MultiResBitmap.Bitmaps[1].LoadFromFile
      (GetHomePath + PathDelim + 'Documents' + PathDelim + edScoreTwo.Text
      + '.jpg');
  End;

  // Play hit sound
  if YouWin = False then
  Begin
    Randomize;

    PlayMedia(MediaPlayerHit, 'Hit' + IntToStr(Random(6) + 1) + '.mp3', stFX);
  End;

  // Check who's serving
  Inc(Points);
  if (Points mod 2) = 0 then
  Begin
    if GlowEffect1.Enabled = False then
    Begin
      GlowEffect1.Enabled := True;
      GlowEffect2.Enabled := False;
    End
    Else
    Begin
      GlowEffect1.Enabled := False;
      GlowEffect2.Enabled := True;
    End;
  End;
end;

procedure TfmMain.MyPopUpOneChange(Sender: TObject);
var
  i: Integer;
Begin
  // Load Image
  ImgPlayerOne.MultiResBitmap.Bitmaps[1].LoadFromFile(GetHomePath + PathDelim +
    'Documents' + PathDelim + MyPopUpOne.Text + '.png');
  if MyPopUpTwo.Text <> '' then
    ImgFight.Visible := True;

  // Load total victories
  PPDBDM.FDTable1.Locate('PLAYER_NAME', MyPopUpOne.Text);
  TotalVictoriesOne.Text := PPDBDM.FDTable1.FieldByName
    ('TOTAL_VICTORIES').AsString;
  TotalDefeatsOne.Text := PPDBDM.FDTable1.FieldByName('TOTAL_DEFEATS').AsString;

  // Store Player One Stats
  StatsPlayerOne[0] := PPDBDM.FDTable1.FieldByName('STATS_SERVICE').AsInteger;
  StatsPlayerOne[1] := PPDBDM.FDTable1.FieldByName('STATS_TECHNIQUE').AsInteger;
  StatsPlayerOne[2] := PPDBDM.FDTable1.FieldByName('STATS_POWER').AsInteger;
  StatsPlayerOne[3] := PPDBDM.FDTable1.FieldByName('STATS_CURVE').AsInteger;
  StatsPlayerOne[4] := PPDBDM.FDTable1.FieldByName('STATS_AGGRESSIVENESS')
    .AsInteger;

  TmrStatsOne.Enabled := True;
End;

procedure TfmMain.MyPopUpTwoChange(Sender: TObject);
Begin
  // Load image
  ImgPlayerTwo.MultiResBitmap.Bitmaps[1].LoadFromFile(GetHomePath + PathDelim +
    'Documents' + PathDelim + MyPopUpTwo.Text + '.png');
  if MyPopUpOne.Text <> '' then
    ImgFight.Visible := True;

  // Load total victories
  PPDBDM.FDTable1.Locate('PLAYER_NAME', MyPopUpTwo.Text);
  TotalVictoriesTwo.Text := PPDBDM.FDTable1.FieldByName
    ('TOTAL_VICTORIES').AsString;
  TotalDefeatsTwo.Text := PPDBDM.FDTable1.FieldByName('TOTAL_DEFEATS').AsString;

  // Store Player Two Stats
  StatsPlayerTwo[0] := PPDBDM.FDTable1.FieldByName('STATS_SERVICE').AsInteger;
  StatsPlayerTwo[1] := PPDBDM.FDTable1.FieldByName('STATS_TECHNIQUE').AsInteger;
  StatsPlayerTwo[2] := PPDBDM.FDTable1.FieldByName('STATS_POWER').AsInteger;
  StatsPlayerTwo[3] := PPDBDM.FDTable1.FieldByName('STATS_CURVE').AsInteger;
  StatsPlayerTwo[4] := PPDBDM.FDTable1.FieldByName('STATS_AGGRESSIVENESS')
    .AsInteger;

  TmrStatsTwo.Enabled := True;
End;

procedure TfmMain.FormActivate(Sender: TObject);
var
  i: Integer;
begin
  // Initialization
  Hitted := 1;
  TabControl1.TabIndex := 0;
  Points := 0;
  cServiceOne := True;
  cTechniqueOne := True;
  cPowerOne := True;
  cCurveOne := True;
  cAggressivenessOne := True;
  cServiceTwo := True;
  cTechniqueTwo := True;
  cPowerTwo := True;
  cCurveTwo := True;
  cAggressivenessTwo := True;

  // PopUp Player One initialization
  MyPopUpOne := TMyPopUpBoxOne.Create(self);
  MyPopUpOne.Parent := fmMain;
  MyPopUpOne.Position.X := 0;
  MyPopUpOne.Position.Y := 0;
  MyPopUpOne.OnChange := MyPopUpOneChange;

  // PopUp Player One initialization
  MyPopUpTwo := TMyPopUpBoxTwo.Create(self);
  MyPopUpTwo.Parent := fmMain;
  MyPopUpTwo.Position.X := 0;
  MyPopUpTwo.Position.Y := 0;
  MyPopUpTwo.OnChange := MyPopUpTwoChange;

  // Fill PopUps
  for i := 0 to PPDBDM.FDTable1.RecordCount - 1 do
  Begin
    MyPopUpOne.Items.Add(PPDBDM.FDTable1.FieldByName('PLAYER_NAME').AsString);
    MyPopUpTwo.Items.Add(PPDBDM.FDTable1.FieldByName('PLAYER_NAME').AsString);
    PPDBDM.FDTable1.Next;
  End;
  MyPopUpOne.Visible := False;
  MyPopUpTwo.Visible := False;
end;

procedure TfmMain.FormShow(Sender: TObject);
begin
  PlayMedia(MediaPlayerMusic, 'Intro.mp3', stMusic);
end;

procedure TfmMain.TabControl1Change(Sender: TObject);
begin
  if TabControl1.ActiveTab.Index = 0 Then
    PlayMedia(MediaPlayerMusic, 'Intro.mp3', stMusic);

  if TabControl1.ActiveTab.Index = 1 Then
    PlayMedia(MediaPlayerMusic, 'Start.mp3', stMusic);

  if TabControl1.ActiveTab.Index = 2 Then
    PlayMedia(MediaPlayerMusic, 'PlayerSelect.mp3', stMusic);
end;

procedure TfmMain.ImgFightClick(Sender: TObject);
begin
  PlayMedia(MediaPlayerMusic, 'Fight.mp3', stMusic);

  ChangeTabAction3.ExecuteTarget(Sender);

  imgPlayerOnePicture.MultiResBitmap.Bitmaps[1].LoadFromFile
    (GetHomePath + PathDelim + 'Documents' + PathDelim + SelectedPlayerOne
    + '.png');
  imgPlayerTwoPicture.MultiResBitmap.Bitmaps[1].LoadFromFile
    (GetHomePath + PathDelim + 'Documents' + PathDelim + SelectedPlayerTwo
    + '.png');

  while MediaPlayerMusic.State = TMediaState.Unavailable do
  Begin

  End;
  PlayMedia(MediaPlayerMusic, 'FightTheme.mp3', stMusic);
end;

procedure TfmMain.ImgPlayerOneClick(Sender: TObject);
begin
  pbServiceOne.Value := 0;
  pbTechniqueOne.Value := 0;
  pbPowerOne.Value := 0;
  pbCurveOne.Value := 0;
  pbAggressivenessOne.Value := 0;
  MyPopUpOne.DoPopup;
end;

procedure TfmMain.ImgPlayerTwoClick(Sender: TObject);
begin
  pbServiceTwo.Value := 0;
  pbTechniqueTwo.Value := 0;
  pbPowerTwo.Value := 0;
  pbCurveTwo.Value := 0;
  pbAggressivenessTwo.Value := 0;
  MyPopUpTwo.DoPopup;
end;

procedure TfmMain.ImgPressStartClick(Sender: TObject);
begin
  TmrPressStart.Enabled := False;
  PlayMedia(MediaPlayerMusic, 'Enter.mp3', stMusic);
end;

procedure TfmMain.imgYouWinClick(Sender: TObject);
begin
  // Game finished, re-initialization
  Hitted := 1;

  ImgFight.Visible := False;

  edScoreOne.Text := '0';
  edScoreTwo.Text := '0';

  imgPointsOneUnits.MultiResBitmap.Bitmaps[1].LoadFromFile
    (GetHomePath + PathDelim + 'Documents' + PathDelim + '0.jpg');
  imgPointsOneDecimals.MultiResBitmap.Bitmaps[1].LoadFromFile
    (GetHomePath + PathDelim + 'Documents' + PathDelim + '0.jpg');
  imgPointsOneDecimals.Visible := False;

  imgPointsTwoUnits.MultiResBitmap.Bitmaps[1].LoadFromFile
    (GetHomePath + PathDelim + 'Documents' + PathDelim + '0.jpg');
  imgPointsTwoDecimals.MultiResBitmap.Bitmaps[1].LoadFromFile
    (GetHomePath + PathDelim + 'Documents' + PathDelim + '0.jpg');
  imgPointsTwoDecimals.Visible := False;

  ChangeTabAction5.ExecuteTarget(Sender);
end;

procedure TfmMain.TmrPressStartTimer(Sender: TObject);
begin
  if ImgPressStart.Visible = True then
    ImgPressStart.Visible := False
  else
    ImgPressStart.Visible := True;
end;

procedure TfmMain.TmrStatsOneTimer(Sender: TObject);
begin
  if pbServiceOne.Value <> StatsPlayerOne[0] then
    pbServiceOne.Value := pbServiceOne.Value + 1
  else
    cServiceOne := False;

  if pbTechniqueOne.Value <> StatsPlayerOne[1] then
    pbTechniqueOne.Value := pbTechniqueOne.Value + 1
  else
    cTechniqueOne := False;

  if pbPowerOne.Value <> StatsPlayerOne[2] then
    pbPowerOne.Value := pbPowerOne.Value + 1
  Else
    cPowerOne := False;

  if pbCurveOne.Value <> StatsPlayerOne[3] then
    pbCurveOne.Value := pbCurveOne.Value + 1
  else
    cCurveOne := False;

  if pbAggressivenessOne.Value <> StatsPlayerOne[4] then
    pbAggressivenessOne.Value := pbAggressivenessOne.Value + 1
  Else
    cAggressivenessOne := False;

  if (cServiceOne = False) and (cTechniqueOne = False) and (cPowerOne = False) and
    (cCurveOne = False) and (cAggressivenessOne = False) then
    TmrStatsOne.Enabled := False;
end;

procedure TfmMain.TmrStatsTwoTimer(Sender: TObject);
begin
  if pbServiceTwo.Value <> StatsPlayerTwo[0] then
    pbServiceTwo.Value := pbServiceTwo.Value + 1
  else
    cServiceTwo := False;

  if pbTechniqueTwo.Value <> StatsPlayerTwo[1] then
    pbTechniqueTwo.Value := pbTechniqueTwo.Value + 1
  else
    cTechniqueTwo := False;

  if pbPowerTwo.Value <> StatsPlayerTwo[2] then
    pbPowerTwo.Value := pbPowerTwo.Value + 1
  Else
    cPowerTwo := False;

  if pbCurveTwo.Value <> StatsPlayerTwo[3] then
    pbCurveTwo.Value := pbCurveTwo.Value + 1
  else
    cCurveTwo := False;

  if pbAggressivenessTwo.Value <> StatsPlayerTwo[4] then
    pbAggressivenessTwo.Value := pbAggressivenessTwo.Value + 1
  Else
    cAggressivenessTwo := False;

  if (cServiceTwo = False) and (cTechniqueTwo = False) and (cPowerTwo = False) and
    (cCurveTwo = False) and (cAggressivenessTwo = False) then
    TmrStatsTwo.Enabled := False;
end;

procedure TfmMain.TmrDelphiLogoTimer(Sender: TObject);
begin
  if ImgSplash.Opacity <> 1 Then
  Begin
    ImgSplash.Opacity := ImgSplash.Opacity + 0.02;
  End
  Else
  Begin
    TmrDelphiLogo.Enabled := False;
    ChangeTabAction1.ExecuteTarget(Sender);
  End;
end;

{ TMyPopUpBox }

procedure TMyPopUpBoxOne.DoPopup;
begin
  inherited;
  self.Visible := False;
end;

procedure TMyPopUpBoxTwo.DoPopup;
begin
  inherited;
  self.Visible := False;
end;

end.
